package com.eostoken.eos_wallet.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseFragment;
import com.eostoken.eos_wallet.helper.DialogHelper;
import com.eostoken.eos_wallet.ui.fragmentfactory.FragmentFactory;
import com.eostoken.eos_wallet.utils.Constants;
import com.eostoken.eos_wallet.utils.TextviewUtils;
import com.fredy.mvp.BasePresenter;
import com.fredy.mvp.BaseView;
import com.fredy.mvp.Model;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import party.loveit.bip44forandroidlibrary.utils.Bip44Utils;

import static com.eostoken.eos_wallet.utils.Constants.BACKUP_MNEMONIC;
import static com.eostoken.eos_wallet.utils.Constants.CONFIRM_MNEMONIC;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_ACCOUNT_NAME;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_HINT;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_WALLET_PASSWORD;

public class BackupMnemonicFragment extends BaseFragment {

    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    @BindView(R.id.mnemonic_text)
    AppCompatTextView mnemonicText;
    @BindView(R.id.backupMnemonicsExplain)
    AppCompatTextView backupMnemonicsExplain;

    List<String> mnemonicCode;
    StringBuilder mnemonicSB;

    private String accountname;
    private String password;
    private String hint;

//    private static class LazyHolder {
//        private static final BackupMnemonicFragment INSTANCE = new BackupMnemonicFragment();
//    }
//
//    public static final BackupMnemonicFragment getInstance(String accountname, String password, String hint) {
//        Bundle bundle = new Bundle();
//        bundle.putString(EXTRA_ACCOUNT_NAME, accountname);
//        bundle.putString(EXTRA_WALLET_PASSWORD, password);
//        bundle.putString(EXTRA_HINT, hint);
//        LazyHolder.INSTANCE.setArguments(bundle);
//        return LazyHolder.INSTANCE;
//    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_backup_mnemonic;
    }

    @Override
    protected void initData() {
        accountname = getArguments().getString(Constants.EXTRA_ACCOUNT_NAME, "");
        password = getArguments().getString(Constants.EXTRA_WALLET_PASSWORD, "");
        hint = getArguments().getString(Constants.EXTRA_HINT, "");
        //TODO 生成助记词
        try {
            mnemonicCode = Bip44Utils.generateMnemonicWords(getActivity());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mnemonicSB = new StringBuilder();
        for (String s : mnemonicCode) {
            mnemonicSB.append(s).append(" ");
        }
        mnemonicText.setText(mnemonicSB.toString().trim());
        LogUtils.dTag(Constants.TAG_MGP, "助记词 [" + mnemonicSB.toString().trim() + "]");
    }

    @Override
    protected void initView(View contentView) {
        topbar.setTitle(StringUtils.getString(R.string.str_backup_mnemonics));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popOrFinish();
            }
        });

        backupMnemonicsExplain.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                backupMnemonicsExplain.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                final String newText = TextviewUtils.autoSplitText(backupMnemonicsExplain, ".");
                if (!TextUtils.isEmpty(newText)) {
                    backupMnemonicsExplain.setText(newText);
                }
            }
        });

        DialogHelper.showLongMessageDialog(getActivity(), getString(R.string.str_backup_mnemonics_dialog_title), getString(R.string.str_backup_mnemonics_dialog_msg), getString(R.string.str_backup_mnemonics_dialog_btn), "", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();
            }
        }, null).show();
    }

    @OnClick(R.id.backupMnemonicsBtn)
    public void onViewClicked() {
        LogUtils.dTag("ConfirmMnemonicFragment==", "accountname = " + accountname + " password = " + password + " hint = " + hint + " mnemonicCode = " + mnemonicCode);
        Fragment fragment = FragmentFactory.getInstatic().getFragment(CONFIRM_MNEMONIC);
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_ACCOUNT_NAME, accountname);
        bundle.putString(EXTRA_WALLET_PASSWORD, password);
        bundle.putString(EXTRA_HINT, hint);
        bundle.putStringArrayList(Constants.EXTRA_WALLET_MNEMONIC, (ArrayList<String>) mnemonicCode);
        fragment.setArguments(bundle);
        FragmentUtils.replace(getFragmentManager(), fragment, R.id.frag_container, "ConfirmMnemonicFragment", true);
//        Intent intent = new Intent(this, ConfirmMnemonicActivity.class);
//        intent.putStringArrayListExtra(Constants.EXTRA_WALLET_MNEMONIC, (ArrayList<String>) mnemonicCode);
//        intent.putExtra(Constants.EXTRA_WALLET_NAME, walletName);
//        intent.putExtra(Constants.EXTRA_WALLET_PASSWORD, walletPassword);
//        intent.putExtra(Constants.EXTRA_WALLET_PASSWORDHIT, walletPasswordHit);
//        ActivityUtils.startActivity(intent);
    }

    @Override
    public Model createModel() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void finishRefresh() {

    }
}
